package com.cpi.basilos.warehouse.action;

import java.util.Locale;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.apache.struts.Globals;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
import org.apache.struts.actions.MappingDispatchAction;
import org.apache.struts.util.MessageResources;

import com.cpi.basilos.unit.exception.UnitException;
import com.cpi.basilos.unit.service.UnitService;
import com.cpi.basilos.unit.view.UnitTransitView;
import com.cpi.basilos.warehouse.exception.WarehouseException;
import com.cpi.basilos.warehouse.form.WarehouseForm;
import com.cpi.basilos.warehouse.service.WarehouseService;
import com.cpi.basilos.warehouse.view.WarehouseView;
import com.cpi.user.view.UserView;
import com.cs.common.exception.CSException;
import com.cs.common.struts.Actionable;

public class WarehouseAction extends MappingDispatchAction implements Actionable {
	public ActionForward stockByCornType(ActionMapping mapping, ActionForm form,HttpServletRequest request, HttpServletResponse response)throws WarehouseException, UnitException {
		String forward = null;
		HttpSession httpSession = null;
		WarehouseForm warehouseForm = null;
		WarehouseService warehouseService = null;
		UnitService unitService = null;
		WarehouseView warehouseView = null;
		UserView userView = null;
		MessageResources messageResources = null;
		String messageError = null;
		UnitTransitView unitTransitView = null;
		try{
			httpSession = request.getSession();
			messageResources = getResources(request);
			warehouseForm = (WarehouseForm)form;
			userView = (UserView)httpSession.getAttribute(USER_SESSION_NAME);
			warehouseService = new WarehouseService();
			unitService = new UnitService();
			warehouseView = warehouseService.getStockByCornType(userView.getWarehouseId());
			warehouseForm.setWarehouseView(warehouseView);
			unitTransitView = unitService.getTransitByOrigin(userView.getWarehouseId());
			warehouseForm.setUnitTransitView(unitTransitView);
			forward = SUCCESS;
		}catch(UnitException unitException){
			messageError = messageResources.getMessage((Locale)httpSession.getAttribute(Globals.LOCALE_KEY), unitException.getErrorCode());
			if(null == messageError){
				httpSession.setAttribute(CUSTOM_EXCEPTION, unitException);
				form.reset(mapping, request);
				throw unitException;
			}else{
				httpSession.setAttribute(ERROR_MESSAGE, unitException);
				forward = FAILS;
			}
		}catch(WarehouseException warehouseException){
			messageError = messageResources.getMessage((Locale)httpSession.getAttribute(Globals.LOCALE_KEY), warehouseException.getErrorCode());
			if(null == messageError){
				httpSession.setAttribute(CUSTOM_EXCEPTION, warehouseException);
				form.reset(mapping, request);
				throw warehouseException;
			}else{
				httpSession.setAttribute(ERROR_MESSAGE, warehouseException);
				forward = FAILS;
			}
		}catch(Exception exception){
			WarehouseException warehouseException = null;
			exception.printStackTrace(System.out);
			warehouseException = new WarehouseException(exception , CSException.LAYER_ACTION,CSException.ACTION_LOGIN);
			form.reset(mapping, request);
			httpSession.setAttribute(CUSTOM_EXCEPTION, warehouseException);
			throw warehouseException;
		}
		return mapping.findForward(forward);
	}
}